package kt.com.bilin.coin.ui.fragment.mill

import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import com.bilin.coin.R
import com.bilin.coin.databinding.ItemMyMillResourceBinding
import kotlinx.android.synthetic.main.activity_base_refresh.*
import kt.com.bilin.coin.api.API_SERVICE
import kt.com.bilin.coin.api.ApiObserver
import kt.com.bilin.coin.api.toData
import kt.com.bilin.coin.bean.MillResourceBean
import kt.com.bilin.coin.common.appendOSSURL
import kt.com.bilin.coin.common.formatDouble
import kt.com.bilin.coin.common.gone
import kt.com.bilin.coin.ui.adapter.BaseRecyclerAdapter
import kt.com.bilin.coin.ui.adapter.BaseViewHolder
import kt.com.bilin.coin.ui.fragment.BaseRefreshFragment
import kt.com.bilin.coin.ui.fragment.launchUI
import kt.com.bilin.coin.utils.GlideUtils
import kt.com.bilin.coin.viewmodel.BackgroundCommonApi

class MillResourceFragment : BaseRefreshFragment<MillResourceBean>() {
    override fun initView(rootView: View?, savedInstanceState: Bundle?) {
        mBaseRecyclerAdapter = MillResourceAdapter()
        super.initView(rootView, savedInstanceState)
        titleView.gone()
        refreshLayout.setEnableLoadMore(false)
    }

    override fun onReload() {
        launchUI({ API_SERVICE.deviceList(mRefreshHelper.page).toData() }, mRefreshHelper.handApiObserver())
    }

    inner class MillResourceAdapter : BaseRecyclerAdapter<BaseViewHolder<ItemMyMillResourceBinding>, MillResourceBean>() {
        override fun onCreateViewHolder(parent: ViewGroup, p1: Int) = BaseViewHolder(ItemMyMillResourceBinding.inflate(getLayoutInflate(parent.context), parent, false))
        override fun onBindViewHolder(holder: BaseViewHolder<ItemMyMillResourceBinding>, bean: MillResourceBean, position: Int) {
            holder.mViewBinding.run {
                bean.run {
                    GlideUtils.intoView(iv1.context, imgUrl.appendOSSURL, iv1)
                    tvCost.text = "租赁所需数量：金豆${costJd.formatDouble()}枚"
                    tvMillName.text = name
                    tvTotalReward.text = awardJd.formatDouble()
                    tvDailyReward.text = perDayAward.formatDouble()
                    tvJx.text = awardJx.formatDouble()
                    tvRewardZz.text = awardZz.formatDouble()
                    tvWorkTimeAndLimit.text = "工作时间：${workTimeStr}  $validDayStr"
                    setViewClick(position, btBuy)
                }
            }
        }

        override fun subOnClickAdapter(v: View, position: Int) {
            val mMyMillBean = getItem(position)
            when (v.id) {
                R.id.bt_buy -> launchUI({ API_SERVICE.buy(mMyMillBean.id).toData() }, ApiObserver(this@MillResourceFragment.activity) { onReload() })
            }
        }
    }
}